import commonjs from "@rollup/plugin-commonjs";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import copyFile from "cpy";
import deleteFile from "del";
import path from "path";
import license from "rollup-plugin-license";
import { terser } from "rollup-plugin-terser";
import typescript from "rollup-plugin-typescript2";

const { type } = process.env; // 命令行入参

const packageRootDir = "packages"; // 包的根目录
const packageDevDir = "src"; // 包的开发目录
const packageOutDir = "lib"; // 包的输出目录

// 获取合法的包 -> 在包根目录下，并且自身有package.json文件
const packageConfig = {
  1: ["hooks", "utils"], // 第一步打包的包
  2: ["event", "react-drag"], // 第二步打包的包，依赖了前面的包
  3: ["swipe"], // 第三步打包的包，依赖了前面的包
};
const packages = packageConfig[type];

// 拷贝一个许可证进去
packages.forEach((packageName) => {
  copyFile(path.join("LICENSE"), path.join(packageRootDir, packageName));
});

// 清空输出目录
const packageOutDirs = packages.map((packageName) => path.join(packageRootDir, packageName, packageOutDir));
deleteFile.sync(packageOutDirs);

// 后缀
const extra = {
  // [后缀, 导出模式]
  hooks: [".tsx", "default"],
  utils: [".ts", "named"],
  event: [".ts", "default"],
  "react-drag": [".tsx", "default"],
  swipe: [".ts", "default"],
};

// 生成打包配置
const configs = [];
packages.forEach((packageValue) => {
  // 入口文件
  let filenames = [];

  if (["hooks", "utils"].includes(packageValue)) {
    // 拷贝ts类型声明到输出目录中
    copyFile(path.join(packageRootDir, packageValue, "types/*"), path.join(packageRootDir, packageValue, packageOutDir));
    if (packageValue === "hooks") {
      // react hooks
      filenames = ["useParamsFreeze", "usePreviousValues"];
    } else if (packageValue === "utils") {
      // js 工具包配置
      filenames = ["dom", "slideTo", "utils"];
    }
  } else if (["event", "react-drag", "swipe"].includes(packageValue)) {
    // 其它包
    filenames = ["index"];
  }

  filenames.forEach((filename) => {
    const inputPathname = path.join(packageRootDir, packageValue, packageDevDir, filename);
    const outputPathname = path.join(packageRootDir, packageValue, packageOutDir, filename);

    configs.push({
      input: `${inputPathname}${extra[packageValue][0]}`,
      output: {
        file: `${outputPathname}.js`,
        format: "cjs",
        exports: extra[packageValue][1],
      },
      external: [
        // 外部引用
        "react",
        "react-dom",
        "@dsh-prose/utils/lib/utils",
        "@dsh-prose/utils/lib/slideTo",
        "@dsh-prose/hooks/lib/useParamsFreeze",
        "@dsh-prose/swipe",
      ],
      plugins: [
        replace({
          // 设置生产环境
          "process.env.NODE_ENV": '"production"',
          preventAssignment: false,
        }),
        nodeResolve({
          extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
        }), // 引入文件
        commonjs(), // commonjs转换为ES6
        typescript({
          // ts配置
          clean: true, // 缓存
          useTsconfigDeclarationDir: true, // 使用ts文件配置
        }),
        terser(), // 压缩精简
        license({
          // 写上作者
          banner: `
            Copyright <%= moment().format('YYYY') %> dsh-94.
          `,
        }),
      ],
    });
  });
});

// 导出配置
export default configs;
